<template>
  <div class="content_page">
    <div class="content_page_logo">
      <image src="/static/myInfo/logo@2x.png"></image>
    </div>
    <div class="content_page_btn">
      <div class="one" @click="toLogin">微信用户快捷登录</div>
	   <div  @click="returnPage" class="two">我再看看</div>
    </div>
  </div>
</template>
<script lang="ts" setup>
// getCurrentInstance
import { onShow } from "@dcloudio/uni-app";
import { ref, getCurrentInstance } from "vue";
import type { resDataType, userinfoType } from "@/api/login/type.ts";
import { loginFn } from "../../api/login/login.ts";
const userInfo = ref<userinfoType>({
  accessToken: "",
  accountId: 0,
  nickname: "",
  avatar: "",
  phoneNumber: "",
});
const instance = getCurrentInstance();
const appStorage = instance?.appContext.config.globalProperties.appStorage;
const fromPage = ref()
const wxSilentLogin = () => {
  return new Promise((resolve, reject) => {
    uni.login({
      success(res) {
        console.log(res, "p1");
        resolve(res.code);
      },
      fail(err) {
        console.log(err, "错误1");
        reject(err);
      },
    });
  });
};
// 获取用户信息
const wxGetUserProfile = () => {
  return new Promise((resolve, reject) => {
    uni.getUserProfile({
      desc: "获取你的昵称、头像、地区及性别",
      success: (res) => {
        resolve(res.userInfo);
        console.log(res, "p2");
      },
      fail: (err) => {
        uni.switchTab({
          url: "/pages/index/index",
        });
        reject(err);
      },
    });
  });
};
const returnPage = ()=>{
	uni.switchTab({
	  url: '/pages/index/index',
	});
}
const toLogin = () => {
  let p1 = wxSilentLogin();
  let p2 = wxGetUserProfile();
  // 等待两个 Promise 都完成
  Promise.all([p1, p2])
    .then((res) => {
      let code = res[0];
      let iv = res[1].iv;
      let encryptedData = res[1].encryptedData;
      const data = {
        code: code,
        encryptedData: encryptedData,
        iv: iv,
      };
      WXLogin(data);
    })
    .catch((err) => {
      console.log(err);
    });
};
const WXLogin = (data: any) => {
  loginFn(data)
    .then((res: resDataType) => {
      if (res.resultStatus) {
        if (appStorage) {
		  res.resultData["smallFont"] = "small-medium";
          appStorage.setObj("userInfo_key", res.resultData);
        }
        if (fromPage.value == "pages/addDevice/addDevice") {
            uni.navigateBack();
        } else {
          uni.switchTab({
          url: "/pages/index/index",
        });
      }
      } else {
        uni.showToast({
          title: res.errorMessage,
          icon: "none",
        });
      }
    })
    .catch((err) => {
      console.log(err);
      uni.showToast({
        title: "服务器异常",
        icon: "none",
      });
    });
};
onShow(() => {
  const pages = getCurrentPages();
  const prevPage = pages[pages.length - 2];
  fromPage.value = prevPage.route;
});
</script>
<style lang="scss" scoped>
.content_page {
  background: #f5f6f7;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  &_logo {
    image {
      width: 360rpx;
      height: 100rpx;
    }
  }

  &_tip {
    font-size: 40rpx;
    text-align: center;
    color: #1f1f1f;
    line-height: 56rpx;
    margin: 30rpx auto 0;
  }

  &_btn {
    margin: 110rpx auto;

    .one {
      width: 500rpx;
      height: 88rpx;
      background: linear-gradient(90deg, #00a67e, #006d56);
      line-height: 88rpx;
      text-align: center;
      border-radius: 44rpx;
      margin-bottom: 30rpx;
      color: #fff;
    }

    .two {
      width: 500rpx;
      height: 88rpx;
      background-color: #ffffff;
      line-height: 88rpx;
      text-align: center;
      border-radius: 44rpx;
    }
  }
}
</style>
